package com.eleventwelve.iris.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.opentides.bean.FileInfo;
import org.opentides.service.FileInfoService;
import org.opentides.util.StringUtil;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.eleventwelve.iris.entity.Patient;
import com.eleventwelve.iris.service.PatientService;

public class DeletePatientFileController extends AbstractController {
	private FileInfoService fileInfoService;
	private PatientService patientService;
	
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		String fileId = request.getParameter("id");
		String patientId = request.getParameter("patientId");
		FileInfo file = null;
		Patient patient = null;
		
		if(!StringUtil.isEmpty(fileId) && StringUtils.isNumeric(fileId)){
			file = fileInfoService.load(fileId);
		}
		
		
		patient = patientService.load(patientId);
		patient.getScannedFiles().remove(file);
		patientService.save(patient);
		fileInfoService.delete(fileId);
		
		return new ModelAndView("redirect:/patient.jspx?id="+patientId+"&item=File&action=delete");
	}


	public FileInfoService getFileInfoService() {
		return fileInfoService;
	}


	public void setFileInfoService(FileInfoService fileInfoService) {
		this.fileInfoService = fileInfoService;
	}


	public PatientService getPatientService() {
		return patientService;
	}


	public void setPatientService(PatientService patientService) {
		this.patientService = patientService;
	}

}
